package day0;

import java.util.Scanner;

public class StudentManager {
	// 输入一组学生的信息Student[] array
	// 从控制台输入
	public static void main(String[] args) {
		Scanner scanner = new Scanner(System.in);
		System.out.print("请输入学生的数量：");
		int count = scanner.nextInt();

		System.out.println("-----------------");// 分割线

		Student[] students = new Student[count];
		
		Input(scanner,students);//调用输入方法
		

		while (true) {
			Syso();//调用查询面板方法

			int type = scanner.nextInt();

			Select(type, students);//调用查询结果方法
			
			if (type == 0) {
				System.out.println("已退出查询");
				break;
			}
			
			System.out.println();

			
			
		}
	}
	
	//输入信息
	public static void Input(Scanner scanner,Student[] students) {
		for (int i = 0; i < students.length; i++) {
			// for循环每遍历一次构造一个学生对象，放到数组里面
			System.out.println("第" + (i + 1) + "个学生");
			Student student = new Student();
			System.out.print("请输入第" + (i + 1) + "个学生的编号：");
			student.id = scanner.nextInt();

			System.out.print("请输入第" + (i + 1) + "个学生的姓名：");
			student.name = scanner.next();

			System.out.print("请输入第" + (i + 1) + "个学生的年龄：");
			student.age = scanner.nextInt();

			System.out.print("请输入第" + (i + 1) + "个学生的班级：");
			student.className = scanner.next();

			System.out.println("");
			students[i] = student;// 把信息放入数组中指定的下标位置中
		}
	}
	
	//打印数组Student[]数组里面所有的学生信息
	public void push(Student[] students) {
		 
			for (int i = 0; i < students.length; i++) {
				Student student = students[i];

				System.out.println(student.id);
				System.out.println(student.name);
				System.out.println(student.age);
				System.out.println(student.className);
			}
	}	
	
	//查询结果面板
	public static void Select(int type, Student[] students) {
		
		Scanner scanner = new Scanner(System.in);

		switch (type) {
		case 1:// 根据id查找
			System.out.print("请输入查找学生id:");
			int id = scanner.nextInt();
			System.out.println("------查询结果------");
			for (int i = 0; i < students.length; i++) {
				Student student = students[i];
				if (id == students[i].id) {
					System.out.println("id为" + id + "的学生的姓名为：" + student.name);
					System.out.println("id为" + id + "的学生的年龄为：" + student.age);
					System.out.println("id为" + id + "的学生的班级为： " + student.className);
				}else {
					System.out.println("未查到id为"+id+"的学生,请重新查询");
				}
			}
			System.out.println("------查询结果------");
			break;
		case 2:// 根据name查找
			System.out.print("请输入查找学生name:");
			String name = scanner.next();
			System.out.println("------查询结果------");
			for (int i = 0; i < students.length; i++) {
				Student student = students[i];
				if (name.equals(students[i].name)) {
					System.out.println("姓名为" + name + "的学生的id为：" + student.id);
					System.out.println("姓名为" + name + "的学生的年龄为：" + student.age);
					System.out.println("姓名为" + name + "的学生的班级为： " + student.className);
				}else {
					System.out.println("未查到姓名为"+name+"的学生,请重新查询");
				}
			}
			System.out.println("------查询结果------");
			break;

		case 3:// 根据年龄查找

			System.out.print("请输入查找学生年龄:");
			int age = scanner.nextInt();
			System.out.println("------查询结果------");
			for (int i = 0; i < students.length; i++) {
				Student student = students[i];
				if (age == students[i].age) {
					System.out.println("年龄为" + age + "的学生的id为：" + student.id);
					System.out.println("年龄为" + age + "的学生的姓名为：" + student.name);
					System.out.println("年龄为" + age + "的学生的班级为： " + student.className);
				}else {
					System.out.println("未查到年龄为"+age+"的学生,请重新查询");
				}
			}
			System.out.println("------查询结果------");
			break;

		case 4:// 根据班级查找
			System.out.print("请输入查找学生班级:");
			String className = scanner.next();
			System.out.println("------查询结果------");
			for (int i = 0; i < students.length; i++) {
				Student student = students[i];
				if (className.equals(students[i].className)) {
					System.out.println("班级为" + className + "的学生的id为： " + student.id);
					System.out.println("班级为" + className + "的学生的姓名为：" + student.name);
					System.out.println("班级为" + className + "的学生的年龄为：" + student.age);
				}else {
					System.out.println("未查到班级为"+className+"的学生,请重新查询");
				}
			}
			System.out.println("------查询结果------");
			break;
		case 0:
			System.out.println("正在退出查询……");
			System.out.println("--*--*--*--*--");
			break;
			 
		default:
			System.out.println("输入错误");
			break;
		}
	}

	//查询面板
	public static void Syso() {
		System.out.println();
		System.out.println("*******查询*******");
		System.out.println("根据id查找：1");
		System.out.println("根据name查找：2");
		System.out.println("根据age查找：3");
		System.out.println("根据className查找：4");
		System.out.println("退出：0");
		System.out.println("*******************");
		System.out.print("输入值：");
	}
}
